#@layout()
#define script()
<script src="#(CPATH)/static/components/vditor/index.min.js"></script>
<script src="#(CPATH)/static/components/ckeditor5/ckeditor.js"></script>
<script src="#(CPATH)/static/components/editable/js/bootstrap-editable.min.js"></script>
<script src="#(CPATH)/static/components/highlight/highlight.pack.js"></script>


<script>

    $(".submitBtn").on("click", function () {
        $("#pageStatus").val($(this).attr("data-status"));
        doSubmit();
    });


    // 全局函数,在CKEditor ctrl + s 快捷键处用到,保留page的status
    window.doSubmit = function () {
        if (window.currentCKEditor){
            $("#content").val(window.currentCKEditor.getData());
        }else if (window.currentVditor){
            $("#content").val(window.currentVditor.getHTML());
        }
        ajaxSubmit("#form", function (data) {
            $("#pageId").attr("value", data.id);
            window.history.replaceState({}, '', `#(CPATH)/admin/page/write/`+data.id)
            toastr.success('页面保存成功。');
        });
    };


    setSaveHotKeyFunction(doSubmit);


    $("#removeThumbnail").on("click", function () {
        $("#thumbnail").attr("src", "#(CPATH)/static/commons/img/nothumbnail.jpg");
        $("#pageThumbnail").val("");
    })


    function doChangeEditor(id) {
        if (confirm('确定要切换编辑器吗？切换可能出现前台渲染内容缺失的问题。')) {
            var changeMode = editMode == "html" ? "markdown" : "html";
            if (id != '0') {
                ajaxGet('#(CPATH)/admin/page/doChangeEditMode?id=' + id + "&mode=" + changeMode);
            } else {
                ajaxGet('#(CPATH)/commons/changeEditor/' + changeMode);
            }
        }
    }

    var editMode = "#(editMode ?? 'html')";
    if (editMode === 'html') {
        initCkEdtior('#editor');
    } else {
        initVdtior('editor');
    }


    $(".chooseAttachment").on("click", function () {
        var $this = $(this);
        layer.open({
            type: 2,
            title: '选择附件',
            anim: 2,
            shadeClose: true,
            shade: 0.3,
            area: ['90%', '90%'],
            content: jpress.cpath + '/admin/attachment/browse?uititle=附件',
            end: function () {
                if (layer.data.src != null) {

                    var src = layer.data.src;
                    //video ".mp4", ".avi", ".wmv", ".mpeg", ".mov", ".mkv", ".flv", ".rmvb", ".rm", ".3gp", ".ts", ".vob", ".m3u8"
                    if (src.endsWith(".mp4") || src.endsWith(".avi") || src.endsWith(".wmv") || src.endsWith(".mpeg") || src.endsWith(".mov")
                        || src.endsWith(".mkv") || src.endsWith(".flv") || src.endsWith(".rmvb") || src.endsWith(".rm") || src.endsWith(".3gp") ||
                        src.endsWith(".ts") || src.endsWith(".vob") || src.endsWith(".m3u8")) {

                        if (typeof currentCKEditor != 'undefined' && currentCKEditor) {
                            var video = document.createElement('video') // 添加的内容可以自定义， 这里以添加图片为例
                            video.setAttribute('src', src)
                            video.setAttribute('style', 'max-width:100%;')
                            video.setAttribute('controls', 'controls')

                            insertHtmlToEditor(video.outerHTML);
                        } else if (currentVditor) {
                            currentVditor.insertValue('附件：[' + layer.data.title + '](' + src + ')')
                        }


                    }

                    //image ".jpg", ".jpeg", ".png", ".bmp", ".gif", ".webp"
                    else if (src.endsWith(".jpg") || src.endsWith(".jpeg") || src.endsWith(".png")
                        || src.endsWith(".bmp") || src.endsWith(".gif") || src.endsWith(".webp")) {

                        if (typeof currentCKEditor != 'undefined' && currentCKEditor) {
                            var img = document.createElement('img') // 添加的内容可以自定义， 这里以添加图片为例
                            img.setAttribute('src', src)
                            insertHtmlToEditor(img.outerHTML);
                        } else if (currentVditor) {
                            //![图片alt](图片地址 ''图片title'')
                            currentVditor.insertValue('![' + layer.data.title + '](' + src + ')')
                        }
                    }
                    // other file
                    else {
                        if (typeof currentCKEditor != 'undefined' && currentCKEditor) {
                            var a = document.createElement('a') // 添加的内容可以自定义， 这里以添加图片为例
                            a.setAttribute('href', src)
                            a.setAttribute("target", "_blank")
                            a.innerHTML = layer.data.title;
                            insertHtmlToEditor('附件：' + a.outerHTML);
                        } else if (currentVditor) {
                            currentVditor.insertValue('附件：[' + layer.data.title + '](' + src + ')')
                        }
                    }
                }
            }
        });
    })

    function insertHtmlToEditor(html) {
        const viewFragment = currentCKEditor.data.processor.toView(html);
        const modelFragment = currentCKEditor.data.toModel(viewFragment);
        currentCKEditor.model.insertContent(modelFragment, currentCKEditor.model.document.selection);
    }


</script>
#end
#define css()
<link href="#(CPATH)/static/components/editable/css/bootstrap-editable.css" rel="stylesheet">
<link href="#(CPATH)/static/components/selectize/css/selectize.css" rel="stylesheet">
<link href="#(CPATH)/static/components/selectize/css/selectize.bootstrap4.css" rel="stylesheet">
<link href="#(CPATH)/static/components/highlight/styles/github.css" rel="stylesheet">
<link href="#(CPATH)/static/components/vditor/index.css" rel="stylesheet">
<style>
    .popover-content {
        padding: 9px 14px;
        margin: 0 10px;
    }

    #slug {
        cursor: pointer;
    }

    .vditor-wysiwyg pre.vditor-reset:focus {
        outline: none;
        background-color: #fff;
    }

    .ck-editor__editable_inline {
        min-height: 500px;
    }

</style>
#end
#define content()

    <section class="content-header">

        <div class="container-fluid">
            <div class="row">
                <div class="col-sm-6">

                    <div class="row mb-2">
                        <div class="col-sm-12">
                            <h1>
                                新建页面
                                <small data-toggle="tooltip" title="" data-placement="right"
                                       data-trigger="hover"><i class="nav-icon far fa-question-circle"></i></small>
                                <small> 首页 / 页面 / 新建页面</small>
                            </h1>
                        </div>
                    </div>
                </div>
            </div>

        </div><!-- /.container-fluid -->

    </section>

    <section class="content">
        <form action="#(CPATH)/admin/page/doWriteSave" method="post" id="form">
            <input type="hidden" id="pageStatus" name="page.status" value='#(page.status ?? "normal")'>
            <input type="hidden" id="pageId" name="page.id" value="#(page.id ??)">
            <input type="hidden" id="editModeId" name="page.edit_mode" value="#(editMode ??)">
            <div class="row">
                <div class="col-lg-9">

                    <!-- /.card-header -->
                    <!-- form start -->
                    <div class="">
                        <div class="form-group row">
                            <div class="col-sm-12">
                                <input type="input" class="form-control form-control-lg" name="page.title" autocomplete="off"
                                       placeholder="请输入标题"
                                       id="page-title"
                                       value="#(page.title ??)">
                            </div>
                        </div>
                        <div class="form-group row">
                            <div class="col-sm-12"style="padding-bottom: 5px;display: flex;align-items: center;justify-content: space-between;">
                                <div>
                                    网址：<span class="domainSpan">#option('web_domain')</span>#(CPATH)/page/<span id="slug"
                                                                                                                   class="slugSpan"
                                                                                                                   for-input="page-slug">#(page.slug ??)</span>#if(option("web_fake_static_enable"))#option('web_fake_static_suffix')#end
                                    #if(page && page.isNormal())
                                    （<a href="#(page.url)" target="_blank">访问</a>）
                                    #end
                                    <input type="hidden" id="page-slug" name="page.slug" value="#(page.slug ??)">
                                </div>


                                <div>
                                    <button type="button" class="btn btn-default btn-sm "
                                            onclick="doChangeEditor('#(page.id ?? 0)')">
                                        <i class="fas fa-exchange-alt"></i>
                                    </button>

                                    <button type="button" class="chooseAttachment btn btn-default btn-sm ">
                                        <i class="fas fa-paperclip"></i> 选择附件
                                    </button>
                                </div>
                            </div>
                            <div class="col-sm-12" style="z-index: 9999;">
                                <div id="editor" >#(page.orignalContent ??)</div>
                                <input type="hidden" name="page.content" id="content"/>
                            </div>
                        </div>
                        <div class="form-group row">
                            <div class="col-sm-12">
                                <div class="card card-outlinecard-solid">
                                    <div class="card-header with-border">
                                        <h3 class="card-title">属性设置</h3>

                                        <div class="card-tools">
                                            <button type="button" class="btn btn-card-tool" data-widget="collapse">
                                                <i class="fa fa-minus"></i>
                                            </button>
                                        </div>
                                    </div>
                                    <!-- /.card-header -->
                                    <div class="card-body">
                                        <!-- Custom Tabs -->

                                        <form class="" autocomplete="off">
                                            <div class="card-body">

                                                <div class="form-group row">
                                                    <label class="col-sm-2 col-form-label">SEO关键字</label>

                                                    <div class="col-sm-6">
                                                    <textarea rows="3" class="form-control" name="page.meta_keywords"
                                                              placeholder="请输入">#(page.meta_keywords ??)</textarea>
                                                    </div>
                                                </div>

                                                <div class="form-group row">
                                                    <label class="col-sm-2 col-form-label">SEO描述</label>

                                                    <div class="col-sm-6">
                                                    <textarea rows="3" class="form-control"
                                                              name="page.meta_description"
                                                              placeholder="请输入">#(page.meta_description ??)</textarea>
                                                    </div>
                                                </div>

                                            </div>

                                        </form>
                                        <!-- nav-tabs-custom -->
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

                <div class="col-lg-3">
                    <div class="card card-outlinecard-solid">
                        <div class="card-body">
                            <button type="button" data-status="normal" class="btn btn-primary btn-sm submitBtn ">发布
                            </button>
                            <button type="button" data-status="draft" class="btn btn-default btn-sm submitBtn">草稿
                            </button>
                            <a href="javascript:;" data-status="trash" class="float-right submitBtn"
                               style="padding-top: 5px;">移至垃圾箱</a>
                        </div>
                    </div>


                    <div class="card card-outlinecard-solid">
                        <div class="card-header with-border ">
                            <h3 class="card-title">模板</h3>
                        </div>
                        <div class="card-body">
                            <div class="form-group row">
                                <select class="form-control" name="page.style">
                                    <option value="">默认（page.html）</option>
                                    #for(style : styles)
                                    <option value="#(style)" #selectedIf(page && page.style== style)>
                                        page_#(style).html
                                    </option>
                                    #end
                                </select>
                            </div>

                            <div class="form-group row">
                                <input type="text" class="form-control" name="page.flag"
                                       placeholder="展示标识" value="#(page.flag ??)" id="flag"/>
                                #if(flags && flags.size() > 0)
                                <p class="text-muted">当前模板支持的展示标识有：</p>
                                <div class="tags">
                                    #for(flag : flags ??)
                                    <a href="javascript:;" class="tflag">#(flag)</a>
                                    #end
                                </div>
                                #end
                            </div>

                        </div>
                    </div>


                    <div class="card card-outlinecard-solid">
                        <div class="card-header with-border ">
                            <h3 class="card-title">缩略图</h3>
                        </div>
                        <div class="card-body p-0">
                            <img src="#(CPATH)#(page.thumbnail ?? '/static/commons/img/nothumbnail.jpg')"
                                 style="width: 100%;max-height: 500px"
                                 id="thumbnail">
                            <input type="hidden" value="#(page.thumbnail ??)" name="page.thumbnail"
                                   id="pageThumbnail">
                        </div>
                        <div class="card-footer">
                            <button type="button" class="btn-image-browser btn btn-default btn-sm "
                                    for-src="thumbnail"
                                    for-input="pageThumbnail">选择图片
                            </button>
                            <a href="javascript:;" style="padding-left: 15px" id="removeThumbnail">移除</a>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </section>

#end
